package com.haiheng.voiceandbook.utils;

import android.app.Dialog;
import android.content.Context;

import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;

import com.haiheng.bookmusic.base.BaseActivity;
import com.haiheng.library.log.LogUtils;
import com.haiheng.voiceandbook.R;


/**
 * 对话框工具类
 *
 * @author weishixiong
 * @Time 2018-07-4
 */
public class SmartDialogUtils extends Dialog {
    private Context mContext;

    public SmartDialogUtils(Context context) {
        super(context, R.style.my_dialog_style);
        mContext = context;
    }

    public static SmartDialogUtils getDialog(Context content, @LayoutRes int resource) {
        final SmartDialogUtils dialog = new SmartDialogUtils(content);
        View view = LayoutInflater.from(content).inflate(resource, new LinearLayout(content));
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        dialog.setContentView(view);
        return dialog;
    }


    public static SmartDialogUtils getDialogNotTouchOutside(Context content,  View view) {
        final SmartDialogUtils dialog = new SmartDialogUtils(content);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(view);

        Window win = dialog.getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = WindowManager.LayoutParams.FILL_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        win.setAttributes(lp);


        return dialog;
    }


    public static SmartDialogUtils getDialogNotTouchOutside(Context content, @LayoutRes int resource) {
        final SmartDialogUtils dialog = new SmartDialogUtils(content);
        View view = LayoutInflater.from(content).inflate(resource, new LinearLayout(content));
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(view);

        Window win = dialog.getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = WindowManager.LayoutParams.FILL_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        win.setAttributes(lp);


        return dialog;
    }

    @Override
    public boolean dispatchTouchEvent(@NonNull MotionEvent ev) {
        if (mContext instanceof  BaseActivity){
            ((BaseActivity) mContext).updateUserActionTime();
        }
        return super.dispatchTouchEvent(ev);
    }
}
